////////////////////////////////////////////////////////////////////////////////
//
//  FLYING KIWI BV
//  Copyright 2010 Flying Kiwi BV
//  All Rights Reserved.
//
//  NOTICE: Flying Kiwi permits you to use, modify, and distribute this file
//  in accordance with the terms of the license agreement accompanying it.
//
////////////////////////////////////////////////////////////////////////////////
package plantool.modules.calendar {
	//--------------------------------------------------------------------------
	//  Imports
	//--------------------------------------------------------------------------
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormatAlign;
	
	import kiwi.components.Button;
	import kiwi.events.CallbackEvent;
	import kiwi.utils.Validation;
	
	import plantool.core.CoreModule;
	import plantool.events.DateEvent;
	import plantool.helpers.TextFieldHelper;
	import plantool.modules.calendar.display.CalendarDay;
	import plantool.modules.calendar.display.WeekButtonSkin;
	import plantool.settings.Settings;
	
	//--------------------------------------------------------------------------
	//  [Metadata (Events, Style, Effects etc)]
	//--------------------------------------------------------------------------
	
	//--------------------------------------------------------------------------
	//  Class declaration
	//--------------------------------------------------------------------------
	public class Calendar extends CoreModule {
		//--------------------------------------------------------------------------
		//Class constants
		//--------------------------------------------------------------------------
		
		//--------------------------------------------------------------------------
		//Class variables
		//--------------------------------------------------------------------------
		private static var _instance:Calendar			= null;
		private var _weekContainer:Sprite				= new Sprite();
		private var _weekBtns:Vector.<Button>			= new Vector.<Button>();
		private var _days:Vector.<CalendarDay>			= new Vector.<CalendarDay>();
		
		//--------------------------------------------------------------------------
		//Class properties
		//--------------------------------------------------------------------------
		
		//--------------------------------------------------------------------------
		//Constructor and Destructor
		//--------------------------------------------------------------------------
		public function Calendar(pvt:PrivateConstructorClass) {
			super();
		}
		
		//--------------------------------------------------------------------------
		//Class methods
		//--------------------------------------------------------------------------
		public static function getInstance():Calendar {
			//check if instance exists, if not create one
			if (!Validation.isSet(_instance)) {
				_instance = new Calendar(new PrivateConstructorClass());
			}
			
			//return instance
			return _instance;
		}
		
		private function onMouse_weekButton(e:CallbackEvent):void {
			if (e.userData.type == MouseEvent.CLICK) {
				dateApi.activeWeek = (parseInt((e.currentTarget as Button).text));
			}
		}
		
		private function onResize_stage(e:Event):void {
			this.x	= 25;
			this.y	= 25;
			
			_weekContainer.x	= (base.stage.stageWidth - _weekContainer.width) / 2;
			_weekContainer.y 	= base.stage.stageHeight - 25 - _weekContainer.height - parseInt(settings.getSetting(Settings.SETTING_GLOBAL_MARGIN).value);
		}

		private function onChange_date(e:DateEvent):void {
			//TODO: Generalize NumDays and show weekend
			var dateList:Vector.<Date> = dateApi.getVisibleDates();
			for (var i:uint = 0; i < dateList.length; i++) {
				if (Validation.isSet(_days[i])) {
					_days[i].date = new Date(dateList[i]);
				}
			}
		}
		
		//TODO: Create variable classes for calender
		protected override function init():void {
			//create days
			var date:Date 		= dateApi.getStartDate();
			var i:uint 			= 0;
			//TODO: Generalize NumDays and show weekend
			var dateList:Vector.<Date> = dateApi.getVisibleDates();
			for (i = 0; i < dateList.length; i++) {
				var day:CalendarDay = new CalendarDay(new Date(dateList[i]));
				day.x				= 145 + (i * 162);
				_days.push(addChild(day));
			}
			
			//create week buttons
			for (i = 1; i < 54; i++) {
				var btn:Button 		= new Button();
				btn.skin			= new WeekButtonSkin();
				btn.width			= 20;
				btn.height			= 20;
				btn.mouseHandler 	= onMouse_weekButton;
				if (i < 10) {
					btn.text = "0" + i.toString();
				} else {
					btn.text = i.toString();
				}
				btn.x = (_weekBtns.length * 20);
				_weekBtns.push(btn);
				_weekContainer.addChild(btn);
			}
			
			//add week container
			base.stage.addChild(_weekContainer);
			
			//listen to page invalidate to position this module
			base.stage.addEventListener(Event.RESIZE, onResize_stage);
			
			dateApi.addEventListener(DateEvent.DATE_CHANGED		, onChange_date);
		}
	}
}


//--------------------------------------------------------------------------
//END OF PACKAGE
//Define the PrivateConstructorClass here so an instance cannot be by anyone
//except for the class defined in the above package
//--------------------------------------------------------------------------
class PrivateConstructorClass {
	//--------------------------------------------------------------------------
	//Constructor and Destructor
	//--------------------------------------------------------------------------
	public function PrivateConstructorClass() {
	}
}